<?php
  require('includes/application_top.php');
  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
  if (tep_not_null($action)) 
  {
    switch ($action) 
	{
      case 'setflag':
        if ( ($HTTP_GET_VARS['flag'] == '0') || ($HTTP_GET_VARS['flag'] == '1') ) 
		{
          if (isset($HTTP_GET_VARS['qID'])) 
		  {
			tep_set_question_status($HTTP_GET_VARS['qID'], $HTTP_GET_VARS['flag']);
          }
        }
        tep_redirect(tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $HTTP_GET_VARS['qID']));
        break;
		
      case 'update':
	    $questions_id = tep_db_prepare_input($HTTP_GET_VARS['qID']);
		$answer = tep_db_prepare_input($HTTP_POST_VARS['answer']);
        $questions_status = tep_db_prepare_input($HTTP_POST_VARS['questions_status']);

        tep_db_query("update " . TABLE_PRODUCT_QUESTIONS . " set status = '" . tep_db_input($questions_status) . "', answer = '" . tep_db_input($answer) . "' where qid = '" . (int)$questions_id . "'");
        tep_redirect(tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $questions_id));
        break;
		
      case 'deleteconfirm':
        $questions_id = tep_db_prepare_input($HTTP_GET_VARS['qID']);
        tep_db_query("delete from " . TABLE_PRODUCT_QUESTIONS . " where qid = '" . (int)$questions_id . "'");
        tep_redirect(tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page']));
        break;
    }
  }
  require(DIR_WS_INCLUDES . 'template_top.php');
?>

    <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
<?php
  if ($action == 'edit')   // start of contents for edit part
  {
    $qID = tep_db_prepare_input($HTTP_GET_VARS['qID']);

    $questions_query = tep_db_query("select qid, product_id, customer_name, customer_email, date_added, question, answer, status from " . TABLE_PRODUCT_QUESTIONS . " where qid = '" . (int)$qID . "'");
    $questions = tep_db_fetch_array($questions_query);

    $products_name_query = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$questions['product_id'] . "' and language_id = '" . (int)$languages_id . "'");
    $products_name = tep_db_fetch_array($products_name_query);

    $qInfo_array = array_merge($questions, $products_name);
    $qInfo = new objectInfo($qInfo_array);

    if (!isset($qInfo->status)) $qInfo->status = '1';
    switch ($qInfo->status) 
	{
      case '0': $in_status = false; $out_status = true; break;
      case '1':
      default: $in_status = true; $out_status = false;
    }
?>
      <tr><?php echo tep_draw_form('questions', FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $HTTP_GET_VARS['qID'] . '&action=preview'); ?>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="main" valign="top"><strong><?php echo ENTRY_PRODUCT; ?></strong> <?php echo $qInfo->products_name; ?><br /><strong><?php echo ENTRY_FROM; ?></strong> <?php echo $qInfo->customer_name; ?><br /><br /><strong><?php echo ENTRY_FROM_EMAIL; ?></strong> <?php echo $qInfo->customer_email; ?><br /><br /><strong><?php echo ENTRY_DATE; ?></strong> <?php echo tep_date_short($qInfo->date_added); ?></td>
          </tr>
          <tr>
            <td class="main" colspan="2"><strong><?php echo TEXT_INFO_QUESTION_STATUS; ?></strong> <?php echo tep_draw_separator('pixel_trans.gif', '24', '15') . '&nbsp;' . tep_draw_radio_field('questions_status', '1', $in_status) . '&nbsp;' . TEXT_QUESTION_PUBLISHED . '&nbsp;' . tep_draw_radio_field('questions_status', '0', $out_status) . '&nbsp;' . TEXT_QUESTION_NOT_PUBLISHED; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
	  
	  <tr>
	  	<td class="main" colspan="2"><strong><?php echo ENTRY_QUESTION; ?></strong> <br /><br /><?php echo $qInfo->question; ?></td>
	  </tr>
	  
	  <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td><table witdh="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td class="main" valign="top"><strong><?php echo ENTRY_ANSWER; ?></strong><br /><br /><?php echo tep_draw_textarea_field('answer', 'soft', '60', '15', $qInfo->answer); ?></td>
          </tr>
          <tr>
            <td class="smallText" align="right"><?php echo ENTRY_QUESTION_TEXT; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td align="right" class="smallText"><?php echo tep_draw_hidden_field('questions_id', $qInfo->qid) . tep_draw_hidden_field('products_id', $qInfo->product_id) . tep_draw_hidden_field('customer_name', $qInfo->customer_name) . tep_draw_hidden_field('customer_email', $qInfo->customer_email) . tep_draw_hidden_field('question', $qInfo->question) .  tep_draw_hidden_field('products_name', $qInfo->products_name)  . tep_draw_hidden_field('date_added', $qInfo->date_added) . tep_draw_button(IMAGE_PREVIEW, 'document') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $HTTP_GET_VARS['qID'])); ?></td>
      </form></tr>
<?php
  } // end of contents for edit part
  
  elseif ($action == 'preview') // start of contents for preview part
  {
    if (tep_not_null($HTTP_POST_VARS)) 
	{
      $qInfo = new objectInfo($HTTP_POST_VARS);
    } 
	else 
	{
      $qID = tep_db_prepare_input($HTTP_GET_VARS['qID']);

      $questions_query = tep_db_query("select qid, product_id, customer_name, customer_email, date_added, question, answer, status from " . TABLE_PRODUCT_QUESTIONS . " where qid = '" . (int)$qID . "'");
      $questions = tep_db_fetch_array($questions_query);

      $products_name_query = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$questions['product_id'] . "' and language_id = '" . (int)$languages_id . "'");
      $products_name = tep_db_fetch_array($products_name_query);

      $qInfo_array = array_merge($questions, $products_name);
      $qInfo = new objectInfo($qInfo_array);
    }
?>
      <tr><?php echo tep_draw_form('update', FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $HTTP_GET_VARS['qID'] . '&action=update', 'post', 'enctype="multipart/form-data"'); ?>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="main" valign="top"><strong><?php echo ENTRY_PRODUCT; ?></strong> <?php echo $qInfo->products_name; ?><br /><strong><?php echo ENTRY_FROM; ?></strong> <?php echo $qInfo->customer_name; ?><br /><br /><strong><?php echo ENTRY_FROM_EMAIL; ?></strong> <?php echo $qInfo->customer_email; ?><br /><br /><strong><?php echo ENTRY_DATE; ?></strong> <?php echo tep_date_short($qInfo->date_added); ?><br /><br /><strong><?php echo ENTRY_QUESTION; ?></strong> <?php echo $qInfo->question; ?></td>
          </tr>
        </table>
      </tr>
      <tr>
        <td><table witdh="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top" class="main"><strong><?php echo ENTRY_ANSWER; ?></strong><br /><br /><?php echo nl2br(tep_db_output(tep_break_string($qInfo->answer, 15))); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
<?php
    if (tep_not_null($HTTP_POST_VARS)) 
	{
	  /* Re-Post all POST'ed variables */
      reset($HTTP_POST_VARS);
      while (list($key, $value) = each($HTTP_POST_VARS)) echo tep_draw_hidden_field($key, htmlspecialchars(stripslashes($value)));
?>
      <tr>
        <td align="right" class="smallText"><?php echo tep_draw_button(IMAGE_SAVE, 'disk', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $qInfo->questions_id)); ?></td>
      </form></tr>
<?php
    } 
	else 
	{
      if (isset($HTTP_GET_VARS['origin'])) 
	  {
        $back_url = $HTTP_GET_VARS['origin'];
        $back_url_params = '';
      } 
	  else
	  {
        $back_url = FILENAME_QUESTIONS;
        $back_url_params = 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $qInfo->qid;
      }
?>
      <tr>
        <td align="right" class="smallText"><?php echo tep_draw_button(IMAGE_BACK, 'triangle-1-w', tep_href_link($back_url, $back_url_params, 'NONSSL')); ?></td>
      </tr>
<?php
    }
  }     // end of contents for preview part
  else  // start of contents for default part
  {
?>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_DATE_ADDED; ?></td>
                <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_STATUS; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
<?php
    $questions_query_raw = "select qid, product_id, date_added, customer_name, question, answer, status from " . TABLE_PRODUCT_QUESTIONS . " order by date_added DESC";
    $questions_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $questions_query_raw, $questions_query_numrows);
    $questions_query = tep_db_query($questions_query_raw);
    while ($questions = tep_db_fetch_array($questions_query))
	{
      if ((!isset($HTTP_GET_VARS['qID']) || (isset($HTTP_GET_VARS['qID']) && ($HTTP_GET_VARS['qID'] == $questions['qid']))) && !isset($qInfo)) 
	  {
        $products_name_query = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$questions['product_id'] . "' and language_id = '" . (int)$languages_id . "'");
        $products_name = tep_db_fetch_array($products_name_query);

        $qInfo_array = array_merge($questions, $products_name);
        $qInfo = new objectInfo($qInfo_array);
      }

      if (isset($qInfo) && is_object($qInfo) && ($questions['qid'] == $qInfo->qid) ) 
	  {
        echo '              <tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $qInfo->qid . '&action=preview') . '\'">' . "\n";
      } 
	  else 
	  {
        echo '              <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $questions['qid']) . '\'">' . "\n";
      }
?>
                <td class="dataTableContent"><?php echo '<a href="' . tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $questions['qid'] . '&action=preview') . '">' . tep_image(DIR_WS_ICONS . 'preview.gif', ICON_PREVIEW) . '</a>&nbsp;' . tep_get_products_name($questions['product_id']); ?></td>
                <td class="dataTableContent" align="right"><?php echo tep_date_short($questions['date_added']); ?></td>
                <td class="dataTableContent" align="center">
<?php
      if ($questions['status'] == '1') 
	  {
        echo tep_image(DIR_WS_IMAGES . 'icon_status_green.gif', IMAGE_ICON_STATUS_GREEN, 10, 10) . '&nbsp;&nbsp;<a href="' . tep_href_link(FILENAME_QUESTIONS, 'action=setflag&flag=0&qID=' . $questions['qid'] . '&page=' . $HTTP_GET_VARS['page']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_status_red_light.gif', IMAGE_ICON_STATUS_RED_LIGHT, 10, 10) . '</a>';
      } 
	  else 
	  {
        echo '<a href="' . tep_href_link(FILENAME_QUESTIONS, 'action=setflag&flag=1&qID=' . $questions['qid'] . '&page=' . $HTTP_GET_VARS['page']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_status_green_light.gif', IMAGE_ICON_STATUS_GREEN_LIGHT, 10, 10) . '</a>&nbsp;&nbsp;' . tep_image(DIR_WS_IMAGES . 'icon_status_red.gif', IMAGE_ICON_STATUS_RED, 10, 10);
      }
?></td>
                <td class="dataTableContent" align="right"><?php if ( (is_object($qInfo)) && ($questions['qid'] == $qInfo->qid) ) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif'); } else { echo '<a href="' . tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $questions['qid']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td>
              </tr>
<?php
    }
?>
              <tr>
                <td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="smallText" valign="top"><?php echo $questions_split->display_count($questions_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_QUESTIONS); ?></td>
                    <td class="smallText" align="right"><?php echo $questions_split->display_links($questions_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page']); ?></td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
<?php
    $heading = array();
    $contents = array();

    switch ($action) {
      case 'delete':
        $heading[] = array('text' => '<strong>' . TEXT_INFO_HEADING_DELETE_QUESTION . '</strong>');

        $contents = array('form' => tep_draw_form('questions', FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $qInfo->qid . '&action=deleteconfirm'));
        $contents[] = array('text' => TEXT_INFO_DELETE_QUESTION_INTRO);
        $contents[] = array('text' => '<br /><strong>' . $qInfo->products_name . '</strong>');
        $contents[] = array('align' => 'center', 'text' => '<br />' . tep_draw_button(IMAGE_DELETE, 'trash', null, 'primary') . tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $qInfo->qid)));
        break;
      default:
      if (isset($qInfo) && is_object($qInfo)) {
        $heading[] = array('text' => '<strong>' . $qInfo->products_name . '</strong>');

        $contents[] = array('align' => 'center', 'text' => tep_draw_button(IMAGE_EDIT, 'document', tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $qInfo->qid . '&action=edit')) . tep_draw_button(IMAGE_DELETE, 'trash', tep_href_link(FILENAME_QUESTIONS, 'page=' . $HTTP_GET_VARS['page'] . '&qID=' . $qInfo->qid . '&action=delete')));
        $contents[] = array('text' => '<br />' . TEXT_INFO_DATE_ADDED . ' ' . tep_date_short($qInfo->date_added));
        $contents[] = array('text' => '<br />' . TEXT_INFO_PRODUCT_AUTHOR . ' ' . $qInfo->customer_name);
		$contents[] = array('text' => '<br />' . TEXT_INFO_PRODUCT_NAME . ' ' . $qInfo->products_name);
		$contents[] = array('text' => '<br />' . TEXT_INFO_PRODUCT_QUESTION . ' ' . $qInfo->question);
		$contents[] = array('text' => '<br />' . TEXT_INFO_PRODUCT_ANSWER . ' ' . $qInfo->answer);
      }
        break;
    }

    if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
      echo '            <td width="25%" valign="top">' . "\n";

      $box = new box;
      echo $box->infoBox($heading, $contents);

      echo '            </td>' . "\n";
    }
?>
          </tr>
        </table></td>
      </tr>
<?php
  } // end of contents for default part
?>
    </table>

<?php
  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>
